package com.three.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.beans.PropertyVetoException;

@Configuration
@ComponentScan(basePackages = {"com.three.service","com.three.dao","com.three.controller"})
@MapperScan(basePackages = "com.three.dao")
@EnableWebMvc
@EnableTransactionManagement
@EnableAspectJAutoProxy
public class SpringConfig {


    @Bean
    public ComboPooledDataSource dataSource(){
        ComboPooledDataSource c3p0 = new ComboPooledDataSource();
        try {
            c3p0.setDriverClass("com.mysql.cj.jdbc.Driver");
            c3p0.setJdbcUrl("jdbc:mysql://106.13.143.56:3306/oadb_group3?useSSL=true");
            c3p0.setUser("group3");
            c3p0.setPassword("group3!");
        } catch (PropertyVetoException e) {
            throw new RuntimeException(e);
        }
        return c3p0;
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(){
        SqlSessionFactoryBean sfb = new SqlSessionFactoryBean();
        sfb.setDataSource(dataSource());
        sfb.setTypeAliasesPackage("com.three.domain");
        return sfb;
    }

    @Bean
    public DataSourceTransactionManager txManager(){
        DataSourceTransactionManager txManager = new DataSourceTransactionManager();
        txManager.setDataSource(dataSource());
        return txManager;
    }
}